package World;

import UserLogin.LogInController;
import CreatePhase.CreatePhaseController;
import Phase.ArkanoidController;
import Phase.ArkanoidPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import javax.swing.JApplet;

public class Application extends JApplet {

    final PrincipalPanel principalPanel = new PrincipalPanel();
    private LogInController logInController;
    private String user;
    private char[] password;

    @Override
    public void init() {
        initComponents();

        this.add(principalPanel);

        principalPanel.getChooseWorldButton().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                createNewArkanoidController();
            }
        });

        principalPanel.getContinueGameButton().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                reloadArkanoidController();
            }
        });

        principalPanel.getLogInButton().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                createUserLogIn();
            }
        });

        principalPanel.getCreatePhase().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                createCreatePhase();
            }
        });
    }

    private void initComponents() {
        this.setSize(500, 500);
        this.setVisible(true);
        this.setFocusable(true);
    }

    public void createNewArkanoidController() {
        principalPanel.setVisible(false);
        ArkanoidPanel arkanoidPanel = new ArkanoidPanel();
        add(arkanoidPanel);

        WorldLoader worldLoader = new FileWorldLoader();
        ArkanoidController arkanoidController = new ArkanoidController(worldLoader.loadWorld(), arkanoidPanel, this);
        arkanoidController.execute();
    }

    private void reloadArkanoidController() {
        principalPanel.setVisible(false);
        this.remove(principalPanel);
        ArkanoidPanel arkanoidPanel = new ArkanoidPanel();
        add(arkanoidPanel);

        ArkanoidController arkanoidController = new ArkanoidController(arkanoidPanel, this);
        arkanoidController.execute();
    }

    public void returnMainMenu() {
        principalPanel.setVisible(true);
    }

    @Override
    public void addKeyListener(KeyListener listener) {
        super.addKeyListener(listener);
        principalPanel.addKeyListener(listener);
    }

    private void createUserLogIn() {
        principalPanel.setVisible(false);
        logInController = new LogInController(this.getWidth(), this.getHeight(), this);
        logInController.execute();
    }

    public void destroyUserLogin() {
        user = logInController.getUser();
        System.out.println(user);
    }

    public PrincipalPanel getPrincipalPanel() {
        return principalPanel;
    }

    private void createCreatePhase() {
        CreatePhaseController createPhaseController = new CreatePhaseController(this);
        createPhaseController.execute();
    }
}
